// 集中管理用户数据
import {
	defineStore
} from "pinia";
import {
	ref
} from "vue";
import {useCartStore} from "./cart";

export const useUserStore = defineStore("user", ()=>{
  const cartStore = useCartStore()
  // 创建用户数据
  const userInfo = ref({});

  const setUserInfo = (user) => {
    userInfo.value = user;
  }

  // 退出时清除用户信息
  const clearUserInfo = () => {
    userInfo.value = {};
    cartStore.clearCart()
  };

  // 以对象形式返回数据和方法
  return {
    userInfo,
    setUserInfo,
    clearUserInfo
  }
}, {
  persist: true
})